package com.zhdl.network.serialport.after;

import cn.hutool.core.util.HexUtil;
import com.project.command.instruction.strategy.abs.AbsAfterControlCommand;
import com.project.command.instruction.strategy.process.data.AfterStrategyParam;
import com.zhdl.modules.autopick.service.CmdHandleService;
import com.zhdl.modules.autopick.service.impl.RobotActionServiceImpl;
import com.zhdl.network.serialport.dto.SerialNormalDto;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service(SerialNormalAfterService.SERIAL_AFTER_SERVICE)
public class SerialNormalAfterService extends AbsAfterControlCommand<SerialNormalDto, ByteBuf> {
    public static final String SERIAL_AFTER_SERVICE = "serial_after_service";

    @Autowired
    private CmdHandleService cmdHandleService;

    /**
     * 后置处理结果判断
     *
     * @param resp
     * @return
     */
    @Override
    public boolean afterResultJudge(AfterStrategyParam resp) {
        List<SerialNormalDto> list = resp.getNewData();
        if (list!=null){
            SerialNormalDto serialNormalDto = list.get(0);
            byte[] fullBuff = serialNormalDto.getFullBuff();

            byte[] distance = new byte[2];
            distance[0] = fullBuff[11];
            distance[1] = fullBuff[10];

            int distance1 = Integer.valueOf(HexUtil.encodeHexStr(distance),16).shortValue();
            log.info("测距测量数据:<<<<<<<<<<<"+ distance1 +"mm");

            RobotActionServiceImpl.pickZ = distance1 * 1000;
            log.info("serial_after_service 接受到数据<<<<<<<<"+
                    HexUtil.encodeHexStr(serialNormalDto.getFullBuff()));
        }
        return true;
    }

    /**
     * 判断 读取 3200 状态字数据
     */
    public String getMotorStatus(SerialNormalDto modbusDto) {
        byte[] unitBuff = modbusDto.getUnitBuff();
        return HexUtil.encodeHexStr(unitBuff);
    }

}
